package com.aisino.progress.config;

import com.aisino.progress.entity.ElEntity;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.*;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Properties;


@Configuration
@ComponentScan("com.aisino.progress")
@PropertySources({@PropertySource(value="classpath:test.properties",encoding = "UTF-8"),@PropertySource(value = {"classpath:com/aisino/progress/test1.properties"},encoding = "UTF-8"),@PropertySource(value = {"classpath:com/aisino/progress/config/test2.properties"},encoding = "UTF-8")})
//@EnableConfigurationProperties({ElEntity.class})
public class Elconfig {
    @Value("I love you")
    private String normal;
    @Value("#{systemProperties['os.name']}")
    private String osName;
    @Value("#{T(java.lang.Math).random()*100.0}")
    private double randomNumber;
    @Value("${book.name}")
    private String proName;
    @Value("${book.color}")
    private String color;
    @Value("#{demoService.another}")
    private String fromAnothher;
    @Value("classpath:test.txt")
    private Resource testFile;

    @Value("http://www.baidu.com")
    private Resource testUrl;
    @javax.annotation.Resource
    private ElEntity elEntity;
    @Autowired
    private Properties properties;
    @Autowired
    private Environment env;
    @Bean
    public Properties properties(){
        return new Properties();
    }

    /*@Bean
    public ElEntity getElEntity(){
       return new ElEntity();
    }*/

    public void outputResource() throws IOException {

        System.out.println(normal);
        System.out.println(osName);
        System.out.println(randomNumber);

        System.out.println(fromAnothher);

        System.out.println(IOUtils.toString(testFile.getInputStream()));
        System.out.println(IOUtils.toString(testUrl.getInputStream()));
        System.out.println(proName);
        System.out.println(color);
        System.out.println(elEntity.getName());
        //Properties properties = new Properties();
        //在jar包中，如果要读取项目路径下的文件，使用File类不好使。读取磁盘文件需要使用绝对路径。
       /* try(FileInputStream inputStream =new FileInputStream("com/aisino/progress/test1.properties")){
            properties.load(inputStream);
            String property = properties.getProperty("book.color");
            System.out.println("测试Properties属性："+elEntity.getName());
        }catch (Exception e){
            e.printStackTrace();
        }*/
        BufferedReader resourceAsStream = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream("../test1.properties"),StandardCharsets.UTF_8));
        properties.load(resourceAsStream);
        String property = properties.getProperty("book.name");
        System.out.println("测试Properties属性："+new String(property.getBytes("UTF-8"),"UTF-8"));

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream("com/aisino/progress/config/test2.properties"),StandardCharsets.UTF_8));
        properties.load(bufferedReader);
        String property2= properties.getProperty("book.love");
        System.out.println("测试Properties属性："+property2);

        ClassPathResource classPathResource = new ClassPathResource("com/aisino/progress/test1.properties");
        InputStream inputStream = classPathResource.getInputStream();
        properties.load(inputStream);
        String property1 = properties.getProperty("book.color");
        System.out.println("测试Properties属性："+property1);

        System.out.println(env.getProperty("school.name"));
        System.out.println(env.getProperty("book.author"));
        System.out.println(env.getProperty("book.love"));
        //需要编码处理，Spring通过ISO8859-1获取的。下面是解决办法（如果是application.yml则不会出现编码格式问题）
        System.out.println(new String(env.getProperty("book.name").getBytes("ISO8859-1"),"UTF-8"));
        System.out.println(env.getProperty("book.saller"));
        System.out.println(env.getProperty("school.addree"));
    }
}
